﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;

namespace FightTheDragon.Managers
{
    class InputManager
    {
        private static InputManager m_Instance = new InputManager();
        private KeyboardState m_KBCurrentState;
        private KeyboardState m_KBprevState;
        private GamePadState m_GPCurrentState;
        private GamePadState m_GPprevState;
        private PlayerIndex m_Controller;
        

        public static InputManager Instance
        {
            get { return m_Instance; }
        }



        

        private InputManager()
        {
            m_KBCurrentState = new KeyboardState();
            m_KBprevState = new KeyboardState();

            m_GPCurrentState = new GamePadState();
            m_GPprevState = new GamePadState();
            m_Controller = PlayerIndex.One;
            m_KBCurrentState = m_KBprevState;
            
        }
        public bool KeyPressedOnce(Keys K)
        {
            
            m_KBCurrentState = Keyboard.GetState();
         

           
            if (m_KBCurrentState.IsKeyDown(K) && !m_KBprevState.IsKeyDown(K))
                return true;

            else
                return false;


            
        }
        public bool KeyDown(Keys K)
        {
            m_KBCurrentState = Keyboard.GetState();

            if (m_KBCurrentState.IsKeyDown(K))
                return true;
            else
                return false;
        }
        public bool ButtonPressedOnce(Buttons B)
        {
            m_GPCurrentState = GamePad.GetState(m_Controller);
            if (m_GPCurrentState.IsButtonDown(B) && !m_GPprevState.IsButtonDown(B))
                return true;
            else
                return false;
           


        }
        public bool ButtonDown(Buttons B)
        {
            m_GPCurrentState = GamePad.GetState(m_Controller);

            if (m_GPCurrentState.IsButtonDown(B))
                return true;

            return false;
        }
        public Vector2 RightStick()
        {
             
             return m_GPCurrentState.ThumbSticks.Right;
        }
        public bool DetermineController()
        {
            for (PlayerIndex index = PlayerIndex.One; index <= PlayerIndex.Four; index++)
            {
                if (m_GPCurrentState.IsButtonDown(Buttons.Start))
                {
                    m_Controller = index;
                    return true;
                    
                }
            }
            return false;
        }

        public void Update()
        {
            m_GPprevState= m_GPCurrentState ;
            m_KBprevState = m_KBCurrentState ;
        }
    }
}
